package com.briup.srms.exam.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.briup.srms.exam.bean.extend.StudentExamExtend;
import com.briup.srms.exam.service.IStudentExamService;
import com.jacky.base.utils.ExcelUtils;
import com.jacky.base.utils.Message;
import com.jacky.base.utils.MessageUtil;
import com.jacky.base.utils.PageVM;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(description="学生考试相关接口")
@Validated
@RestController
@RequestMapping("/exam/se")
public class StudentExamController {

	@Autowired
	private IStudentExamService studentExamService;

	@ApiOperation(value = "分页多条件获取学生考试信息")
	@GetMapping("pageQuery")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
		@ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query"),
		@ApiImplicitParam(name = "status", value = "学生考试状态", required = false, paramType = "query"),
		@ApiImplicitParam(name = "studentId", value = "学生编号", required = false, paramType = "query"),
		@ApiImplicitParam(name = "examId", value = "考试编号", required = false, paramType = "query"),
		@ApiImplicitParam(name = "clazzId", value = "班级编号", required = false, paramType = "query"),
	})
	public Message query(int page, int pageSize,String status,Long studentId,Long examId,Long clazzId) {
		PageVM<StudentExamExtend> list = studentExamService.query(page, pageSize, status,studentId,examId,clazzId);
		return MessageUtil.success(list);
	}

	@ApiOperation(value = "将学生成绩导出到Excel中", notes = "注意！测试的时候请将地址粘贴到浏览器地址栏测试", produces = "application/octet-stream")
	@GetMapping("downloadStudentScore")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
		@ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query"),
		@ApiImplicitParam(name = "status", value = "学生考试状态", required = false, paramType = "query"),
		@ApiImplicitParam(name = "studentId", value = "学生编号", required = false, paramType = "query"),
		@ApiImplicitParam(name = "examId", value = "考试编号", required = false, paramType = "query"),
		@ApiImplicitParam(name = "clazzId", value = "班级编号", required = false, paramType = "query"),
	})
	public void downloadStudentScore(HttpServletResponse response, int page, int pageSize,String status,Long studentId,Long examId,Long clazzId) throws Exception {
		// 2. 将excel导出
		// 2.1 excel头部数据
		String[] headList = new String[] { "学号","姓名", "班级","主观题成绩", "客观题成绩", "总成绩" };
		// 2.2 属性
		String[] fieldList = new String[] { "schoolStuNo","realname","clazz","scoreSubjective","scoreObjective","score"};
		// 2.3 数据列，需要转化
		List<Map<String, Object>> dataList = new ArrayList<>();

		PageVM<StudentExamExtend> pageVm = studentExamService.query(page, pageSize, status,studentId,examId,clazzId);

		for (StudentExamExtend u : pageVm.getList()) {
			Map<String, Object> map = new HashMap<>();
			map.put("schoolStuNo", u.getStudent()!=null?u.getStudent().getSchoolStuNo():"");
			map.put("realname", u.getStudent()!=null?u.getStudent().getRealname():"");
			map.put("clazz", u.getStudent()!=null?u.getStudent().getClazz():"");
			map.put("scoreSubjective", u.getScoreSubjective());
			map.put("scoreObjective", u.getScoreObjective());
			map.put("score", u.getScore());
			dataList.add(map);
		}
		// 2.4 导出文件名
		String excelName = "student_score_list";
		// 3. 调用工具方法导出Excel
		ExcelUtils.createExcel(response, excelName, headList, fieldList, dataList);

	}

	@ApiOperation(value = "通过id查询学生考试详情")
	@GetMapping("queryById")
	public Message queryById(Long id) {
		StudentExamExtend se = studentExamService.queryById(id);
		return MessageUtil.success(se);
	}



}
